//package com.xb.system.service.impl;
//
//import com.google.common.hash.BloomFilter;
//import com.xb.model.domain.Users;
//import com.xb.system.constants.BatchConstants;
//import com.xb.system.constants.ThreadPoolManager;
//import com.xb.system.redis.UserPhoneBloomFilterConfig;
//import com.xb.system.service.DynamicDataSplitterService;
//import com.xb.system.service.FileHandler;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.io.BufferedReader;
//import java.io.FileReader;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
//@Slf4j
//@Component
//public class DoubleBufferTxtFileHandler implements FileHandler {
//
//    @Value("${file.batch.size}")
//    private int batchSize;
//
//    @Resource
//    private DynamicDataSplitterService dynamicDataSplitterService;
//    @Resource
//    private BatchProcessor batchProcessor;
//    
//    @Resource
//    private UserPhoneBloomFilterConfig userPhoneBloomFilterConfig;
//
//    @Override
//    public void handleFile(String file) throws IOException {
//        BloomFilter<String> bloomFilter = userPhoneBloomFilterConfig.createBloomFilter();
//        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
//            String line;
//
//            while ((line = reader.readLine()) != null) {
//                List<Users> currentBatch;       // 当前批次
//                List<Users> alternateBatch;     // 备用批次
//                //双Buffer模式，参考https://tech.meituan.com/2017/04/21/mt-leaf.html
//                //两个集合交替读取，读满了第一个再读到第二个
//                synchronized (BatchConstants.class) {
//                    if (BatchConstants.USE_BUFFER_1) {      // 默认使用 BATCH_BUFFER_1作为当前批次
//                        currentBatch = BatchConstants.BATCH_BUFFER_1;
//                        alternateBatch = BatchConstants.BATCH_BUFFER_2;
//                    } else {
//                        currentBatch = BatchConstants.BATCH_BUFFER_2;
//                        alternateBatch = BatchConstants.BATCH_BUFFER_1;
//                    }
//                }
//
//                synchronized (currentBatch) {
//                    while (currentBatch.size() >= batchSize) {
//                        //当这个判断成立，currentBatch实际上是上一次的alternateBatch批次
//                        //说明两个集合都满了，这里需要等待一下
//                        try {
//                            currentBatch.wait();
//                        } catch (InterruptedException e) {
//                            Thread.currentThread().interrupt();
//                            log.error("批处理等待时被中断", e);
//                        }
//                    }
//
//                    dynamicDataSplitterService.processLine(line.trim(), currentBatch,bloomFilter);
//
//                    if (currentBatch.size() >= batchSize) {
//                        synchronized (BatchConstants.class) {
//                            BatchConstants.USE_BUFFER_1 = !BatchConstants.USE_BUFFER_1;
//                        }
//
//                        synchronized (alternateBatch) {
//                            alternateBatch.notifyAll();
//                        }
//                    }
//                }
//            }
//
//            // 处理最后剩余的数据
//            synchronized (BatchConstants.class) {
//                List<Users> remainingBatch = BatchConstants.USE_BUFFER_1 ? BatchConstants.BATCH_BUFFER_1 : BatchConstants.BATCH_BUFFER_2;
//                if (!remainingBatch.isEmpty()) {
//                    batchProcessor.processingSmallData(new ArrayList<>(remainingBatch));
//                    remainingBatch.clear();
//                }
//            }
//
//        } catch (IOException e) {
//            log.error("读取文件时发生错误：{}", e.getMessage());
//        } finally {
//            ThreadPoolManager.shutdown();
//        }
//    }
//}
